package com.aierkeji.call.cms.service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.transaction.Transactional;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.dao.jpa.AkjCallEnterpriseDao;
import com.aierkeji.call.cms.dao.jpa.AkjCallUserDao;
import com.aierkeji.call.cms.entity.AkjCallEnterprise;
import com.aierkeji.call.cms.entity.AkjCallUser;
import com.aierkeji.call.cms.entity.snap.AkjCallUserSnap;
import com.aierkeji.call.cms.util.DateTimeUtils;
import com.aierkeji.call.cms.util.DynamicSpecifications;
import com.aierkeji.call.cms.util.PageUtils;
import com.aierkeji.call.cms.util.ResultMapHelper;
import com.aierkeji.call.cms.util.SearchFilter;
import com.aierkeji.call.cms.util.StringUtils;

@Service
public class AkjCallEnterpriseService {

	@Autowired
	AkjCallEnterpriseDao akjCallEnterpriseDao;
	
	@Autowired
	AkjCallUserDao akjCallUserDao ; 

	public Map<String, Object> findEnterpriseByCreatedBy(int pageNo, Map<String, Object> map) {
		AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
		Order updatedAt = Order.desc("updatedAt");
		Order createdAt = Order.desc("createdAt");
		Sort sort = Sort.by(updatedAt, createdAt);
		Pageable pageable = PageUtils.buildPageRequest(pageNo, Contants.PAGE_SIZE, sort);
		if (akjCallUser.checkRole(Contants.ROLE_ID_MANAGER)) {
			map.put("JOINEQ_createdBy.id", akjCallUser.getId());
		}
		map.put("EQ_isDelete", false);

		Map<String, SearchFilter> searchMap = SearchFilter.parse(map);
		Specification<AkjCallEnterprise> specification = DynamicSpecifications.bySearchFilter(searchMap.values(),
				AkjCallEnterprise.class);
		Page<AkjCallEnterprise> page = this.akjCallEnterpriseDao.findAll(specification, pageable);
		return ResultMapHelper.success(page);
	}

	@Transactional
	public Map<String, Object> edit(Long id, Map<String, Object> map) {
		// TODO Auto-generated method stub
		AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
		String name = (String) map.get("name");
		if (StringUtils.isEmpty(name)) {
			return ResultMapHelper.nullParamsResult();
		}
		Optional<AkjCallEnterprise> optional = akjCallEnterpriseDao.findById(id);
		if (optional.isPresent()) {
			AkjCallEnterprise akjCallEnterprise = optional.get();
			if (akjCallEnterprise.getCreatedBy().getId().equals(akjCallUser.getId())
					|| akjCallUser.checkRole(Contants.ROLE_ID_ADMIN)) {
				if (!name.equals(akjCallEnterprise.getName())) {
					akjCallEnterprise.setName(name);
					akjCallEnterprise.setUpdatedAt(DateTimeUtils.getCurrentTime());
					akjCallEnterprise.setUpdatedBy(AkjCallUserSnap.build(akjCallUser));
					this.akjCallEnterpriseDao.save(akjCallEnterprise);
				}
				return ResultMapHelper.success();
			}
		} else {
			return ResultMapHelper.nullParamsResult();
		}
		return ResultMapHelper.failure("修改企业信息失败!");
	}

	@Transactional
	public Map<String, Object> add(Map<String, Object> map) {
		// TODO Auto-generated method stub
		AkjCallUser akjCallUser = (AkjCallUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
		String name = (String) map.get("name");
		if (StringUtils.isEmpty(name)) {
			return ResultMapHelper.nullParamsResult();
		}
		AkjCallEnterprise akjCallEnterprise = new AkjCallEnterprise();
		akjCallEnterprise.setName(name);
		akjCallEnterprise.setCreatedAt(DateTimeUtils.getCurrentTime());
		akjCallEnterprise.setCreatedBy(AkjCallUserSnap.build(akjCallUser));
		this.akjCallEnterpriseDao.save(akjCallEnterprise);
		Optional<AkjCallUser> optional = akjCallUserDao.findById(akjCallUser.getId());
		if(optional.isPresent()) {
			AkjCallUser newAkjuser = optional.get();
			newAkjuser.setEnterprise(akjCallEnterprise);
			this.akjCallUserDao.save(newAkjuser);
		}
		return ResultMapHelper.success();
	}
}
